<?php

/**
 * Validates find form.
 *
 * @package    symfony
 * @subpackage validator
 * @author     Emeric Chevillard
 * @version    SVN: 
 */
class ValidatorTachePlanning extends sfValidatorBase {
	public function __construct($field, $options = array(), $messages = array()) {
		$this->addOption('field', $field);
		parent::__construct($options, $messages);
	}
	
	/**
	 * @see sfValidatorBase
	 */
	public function configure($options = array(), $messages = array()) {
		$this->setMessage('invalid', 'Date invalide');
	}

	/**
	 * @see sfValidatorBase
	 */
	protected function doClean($values) {
		$phase = Doctrine::getTable('planning_phase')->find($values['plntac_plnphs_id'], Doctrine_Core::HYDRATE_RECORD);
		switch ($this->getOption('field')) {
			case 'plntac_date_debut':
				if ($values['plntac_date_debut'] < $phase->getPlnphsDateDebut()) {
					$this->setMessage('invalid', 'La date de début doit être égale ou postérieure à celle de la phase ['.$phase->getPlnphsDateDebut().']');
					throw new sfValidatorErrorSchema($this, array(
						'plntac_date_debut' => new sfValidatorError($this, 'invalid')
					));
				}
				break;
			case 'plntac_date_echeance':
				if ($values['plntac_date_echeance'] != '') {
					if ($phase->getPlnphsDateEcheance() != '' && $values['plntac_date_echeance'] > $phase->getPlnphsDateEcheance()) {
						$this->setMessage('invalid', 'La date d\'échéance doit être égale ou antérieure à celle de la phase ['.$phase->getPlnphsDateEcheance().']');
						throw new sfValidatorErrorSchema($this, array(
							'plntac_date_echeance' => new sfValidatorError($this, 'invalid')
						));
					} 
					if ($values['plntac_date_echeance'] < $values['plntac_date_debut']) {
						$this->setMessage('invalid', 'La date d\'échéance est antérieure à celle de début');
						throw new sfValidatorErrorSchema($this, array(
							'plntac_date_echeance' => new sfValidatorError($this, 'invalid')
						));
					}
				}
				break;
		}
		return $values;
	}
}
